package com.ecloud.arithmetic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.arithmetic.dao.EnergyIndicatorsDailyMapper;
import com.ecloud.arithmetic.dto.EnergyIndicatorsDailyDto;
import com.ecloud.arithmetic.dto.EnergyIndicatorsDailySelectDto;
import com.ecloud.arithmetic.entities.EnergyIndicatorsDaily;
import com.ecloud.arithmetic.service.EnergyIndicatorsDailyService;
import com.ecloud.common.ResultEnum;
import com.ecloud.common.exception.ServerException;
import com.ecloud.common.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Squbi
 */
@Service
public class EnergyIndicatorsDailyServiceImpl
        extends ServiceImpl<EnergyIndicatorsDailyMapper, EnergyIndicatorsDaily>
        implements EnergyIndicatorsDailyService {

    @Autowired
    EnergyIndicatorsDailyMapper dailyMapper;

    @Override
    public void insert(EnergyIndicatorsDailyDto dto) {
        EnergyIndicatorsDaily eid;
        try {
            eid = dto.toEntities(new EnergyIndicatorsDaily());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new ServerException(ResultEnum.ARGS_ERR, e.getMessage());
        }
        eid.setDataTime(TimeUtil.fromDateStr(dto.getDataDailyTime()));
        eid.setCreateTime(LocalDateTime.now());
        dailyMapper.insert(eid);
    }

    @Override
    public List<EnergyIndicatorsDaily> select(EnergyIndicatorsDailySelectDto dto) {
        dto.toLimit();
        return dailyMapper.selectPage(dto);
    }
}
